package it.koutifaris.travianmap.dal.sqlservices;

import static it.koutifaris.travianmap.dal.sqlservices.Auditable.COLUMN_NAME_REVISION_ID;
import static it.koutifaris.travianmap.dal.sqlservices.MapSqlContract.Players.COLUMN_NAME_ALLIANCE_ID;
import static it.koutifaris.travianmap.dal.sqlservices.MapSqlContract.Players.COLUMN_NAME_PLAYER_ID;
import static it.koutifaris.travianmap.dal.sqlservices.MapSqlContract.Players.COLUMN_NAME_PLAYER_NAME;
import static it.koutifaris.travianmap.dal.sqlservices.MapSqlContract.Players.TABLE_NAME;
import junit.framework.Assert;
import android.content.ContentValues;
import android.database.SQLException;

public class PlayersTableTest extends AbstractMapDbTest {

	public void testPlayersTableExist() {
		getDbHelper().getReadableDatabase().rawQuery("select 1 from " + TABLE_NAME, null);
	}

	public void testUniqueConstraint() {
		ContentValues contentValues = new ContentValues();

		setUniqueKeyData(contentValues);
		setVillageData(contentValues);

		getDbHelper().getWritableDatabase().insertOrThrow(TABLE_NAME, null, contentValues);
		try {
			changePlayerData(contentValues);
			getDbHelper().getWritableDatabase().insertOrThrow(TABLE_NAME, null, contentValues);

			Assert.fail("Unique key on " + COLUMN_NAME_REVISION_ID + " and " + COLUMN_NAME_PLAYER_ID + " expected");
		} catch (SQLException e) {
			// OK, test passed
		}

	}

	private void setUniqueKeyData(ContentValues contentValues) {
		contentValues.put(COLUMN_NAME_REVISION_ID, 1);
		contentValues.put(COLUMN_NAME_PLAYER_ID, 1);
	}

	private void setVillageData(ContentValues contentValues) {
		contentValues.put(COLUMN_NAME_PLAYER_NAME, "Test Player");
		contentValues.put(COLUMN_NAME_ALLIANCE_ID, 1);
	}

	private void changePlayerData(ContentValues contentValues) {
		contentValues.put(COLUMN_NAME_PLAYER_NAME, "Test Player B");
		contentValues.put(COLUMN_NAME_ALLIANCE_ID, 2);
	}
}
